package com.rongzhe.house.manager;

import android.text.TextUtils;

import com.baidu.location.BDLocation;
import com.rongzhe.house.entity.HouseCriteriaTo;
import com.rongzhe.house.entity.HouseEntrustTo;
import com.rongzhe.house.entity.ResponseEntityVo;
import com.rongzhe.house.entity.voo.HouseDetail;
import com.rongzhe.house.entity.voo.HouseOverview;
import com.rongzhe.house.internet.DataListener;
import com.rongzhe.house.internet.InternetRequestWorker;
import com.rongzhe.house.internet.ResponseDispatcher;
import com.rongzhe.house.internet.net.HouseInterface;
import com.rongzhe.house.ui.adapter.MainRentTypeAdapter;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by huang on 2017/11/22.
 */
public class HouseManager {
    private HouseManager() {
    }

    private static HouseManager sInstance;
    private HouseCriteriaTo mCurrentQueryFilter = new HouseCriteriaTo();
    private HouseDetail mCurrentHouseDetail = null;


    public void clearAllFilter() {
        mCurrentQueryFilter = new HouseCriteriaTo();
    }

    public void setCity(String cityId) {
        mCurrentQueryFilter.setCityId(cityId);
    }

    public HouseDetail getCurrentHouseDetail() {
        return mCurrentHouseDetail;
    }

    public void setQueryRegion(String rejion) {
        mCurrentQueryFilter.setCityId(PositionManager.getInstance().getCurrentCityId());
        mCurrentQueryFilter.setCountyId(rejion);
    }

    public void setSubway(String subway) {
        mCurrentQueryFilter.setRoute(subway);
    }

    public void setSearchNearby(boolean findRound, BDLocation bdLocation) {
        mCurrentQueryFilter.setFindRound(findRound);
        if (findRound) {
            mCurrentQueryFilter.setLat(bdLocation.getLatitude());
            mCurrentQueryFilter.setLon(bdLocation.getLongitude());
            // remove other position filter.
            mCurrentQueryFilter.setCityId(null);
            mCurrentQueryFilter.setCountyId(null);
            mCurrentQueryFilter.setRoute(null);
        }
    }

    public void setAsRentDirect(Boolean direct) {
        mCurrentQueryFilter.setAsOwnerRent(direct);
    }
    public void setAsEntire(Boolean entire) {
        mCurrentQueryFilter.setAsEntireRent(entire);
    }
    public void setAsApartment(Boolean apartment) {
        mCurrentQueryFilter.setAsApartMent(apartment);
    }
    public boolean hasTypeFilter() {
        return mCurrentQueryFilter.getEntireRent() != null;
    }

    public void setAsNoTypeFilter() {
        mCurrentQueryFilter.setAsEntireRent(null);
    }

    public void addHouseQueryType(String type) {
        mCurrentQueryFilter.getRoomNumList().add(type);
    }

    public void clearHouseType() {
        mCurrentQueryFilter.getRoomNumList().clear();
    }

    public void setKeywords(String keywords) {
        mCurrentQueryFilter.setKeyword(keywords);
    }

    public void setTime(String time) {
        mCurrentQueryFilter.setEnterTime(time);
    }

    public boolean hasTimeFilter() {
        return !TextUtils.isEmpty(mCurrentQueryFilter.getEnterTime());
    }

    public void setRentStart(BigDecimal bigDecimal) {
        mCurrentQueryFilter.setRentStart(bigDecimal);
    }

    public void setRentEnd(BigDecimal bigDecimal) {
        mCurrentQueryFilter.setRentEnd(bigDecimal);
    }

    public boolean hasRentFilter() {
        return mCurrentQueryFilter.getRentStart() != null
                || mCurrentQueryFilter.getRentEnd() != null;
    }

    public boolean hasPositionFilter() {
        return mCurrentQueryFilter.getCountyId() != null
                || mCurrentQueryFilter.getRoute() != null
                || (mCurrentQueryFilter.getFindRound() != null && (mCurrentQueryFilter.getFindRound()));
    }

    public static HouseManager getInstance() {
        if (sInstance == null) {
            sInstance = new HouseManager();
        }
        return sInstance;
    }

    public void getFirstPageHouse(final String cityCode,
                                  final DataListener<List<HouseOverview>> dataListener) {
        HouseCriteriaTo houseCriteria = new HouseCriteriaTo();
        houseCriteria.setCityId(cityCode);
        InternetRequestWorker.getInstance().asyncNetwork(InternetRequestWorker.getInstance()
                .create(HouseInterface.class).firstPage(houseCriteria), new ResponseDispatcher(dataListener));
    }

    public void getHouseDetail(final int houseId, final DataListener<HouseDetail> dataListener) {
        InternetRequestWorker.getInstance().asyncNetwork(InternetRequestWorker.getInstance()
                        .create(HouseInterface.class).getDetail(houseId + ""),
                new ResponseDispatcher(dataListener) {
                    @Override
                    protected void handleSuccess(ResponseEntityVo data) {
                        if (data.success()) {
                            mCurrentHouseDetail = (HouseDetail) data.getData();
                        }
                    }
                });
    }

    public void queryHouse(final int pageSize, final int currentPage,
                           final DataListener<List<HouseOverview>> dataListener,
                           HouseCriteriaTo filter) {
        filter.setPageSize(pageSize);
        filter.setCurrentPage(currentPage);
        InternetRequestWorker.getInstance().asyncNetwork(InternetRequestWorker.getInstance()
                        .create(HouseInterface.class).queryHouse(filter),
                new ResponseDispatcher(dataListener));
    }

    public void queryHouse(final int pageSize, final int currentPage,
                           final DataListener<List<HouseOverview>> dataListener) {
        this.queryHouse(pageSize, currentPage, dataListener, mCurrentQueryFilter);
    }

    public void updateHouseStarState(final int houseId, final DataListener<Object> dataListener) {
        Map<String, String> data = new HashMap<>();
        data.put("houseId", houseId + "");
        InternetRequestWorker.getInstance().asyncNetwork(InternetRequestWorker.getInstance()
                .create(HouseInterface.class).changeStarState(data), new ResponseDispatcher(dataListener));
    }

    public void getStaredHouse(final int pageSize, final int currentPage,
                               final DataListener<List<HouseOverview>> dataListener) {
        InternetRequestWorker.getInstance().asyncNetwork(InternetRequestWorker.getInstance()
                        .create(HouseInterface.class).getStaredHouse(pageSize, currentPage),
                new ResponseDispatcher(dataListener));
    }

    public void houseEntrust(final HouseEntrustTo entrustTo,
                             final DataListener<Object> dataListener) {
        InternetRequestWorker.getInstance().asyncNetwork(InternetRequestWorker.getInstance()
                        .create(HouseInterface.class).houseEntrust(entrustTo),
                new ResponseDispatcher(dataListener));
    }
}
